﻿using Common.Data;
using Common.Service.DataTables;
using SMECustomerWebsite.Jobs;
using SMECustomerWebsite.Models;
using SMECustomerWebsite.Models.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace SMECustomerWebsite
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        private WebDbContextStorage _storage;
        public override void Init()
        {
            base.Init();
            _storage = new WebDbContextStorage(this);
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            ModelBinders.Binders.Add(typeof(DataTablesPageRequest), new DataTablesModelBinder());
            ModelBinders.Binders.Add(typeof(IStepViewModel), new StepViewModelBinder());
            //WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
            // Setup profiler for Controllers via a Global ActionFilter
            //GlobalFilters.Filters.Add(new ProfilingActionFilter());

            //JobScheduler.Start();
            //// initialize automatic view profiling
            //var copy = ViewEngines.Engines.ToList();
            //ViewEngines.Engines.Clear();
            //foreach (var item in copy)
            //{
            //    ViewEngines.Engines.Add(new ProfilingViewEngine(item));
            //    //ViewEngines.Engines.Add(MiniProfiler.Current.User);
            //}
        }
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            DbContextInitializer.Instance().InitializeDbContextOnce(() =>
            {
                DbContextManager.InitStorage(_storage);
                //DbContextManager.Init(new[] { Server.MapPath("~/bin/Authentication.dll") }, false);
            });
            //if (Request.IsLocal)
            //{
            //    MiniProfiler.Start();
            //}
        }
        protected void Application_EndRequest()
        {
            //MiniProfiler.Stop();
        }
    }
}